<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="assets/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="assets/css/right.css" rel="stylesheet">
    <script src="assets/jquery-3.5.1.min.js"></script>
    <script src="assets/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    <script src="assets/vue.min-v2.5.16.js"></script>
    <script src="assets/vue-router.min-2.7.0.js"></script>
    <script src="assets/axios.min.js"></script>
    <script src="assets/date_picker.js"></script>
</head>
<body>
    <div id="app" class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="row">
                    <div class="col-md-12" style="text-align: center; font-weight: bold; font-size: 18px; height: 80px; line-height: 80px;">
                        {{title}}
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <label>业主姓名</label>
                        <input type="text" class="form-control" v-model="owner.username">
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <label>性别</label>
                        <input type="text" class="form-control" v-model="owner.sex"/>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <label>联系方式</label>
                        <input type="text" class="form-control" v-model="owner.tel"/>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <label>身份证号</label>
                        <input type="text" class="form-control" v-model="owner.identity"/>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <label>房屋编号</label>
                        <select class="form-control" v-model="owner.houseId">
                            <option v-for="b in houselist" :value="b.id">{{b.numbers}}</option>
                        </select>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-6 col-md-offset-3" style="height: 80px;">
                        <button class="btn btn-primary" @click="doSave">保存</button>
                        <button class="btn btn-default" @click="doCancel">取消</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        new Vue({
            el: '#app',
            data: {
                title: null,
                userId: null,
                owner:{
                    id:null,
                    username:null,
                    sex:null,
                    tel:null,
                    identity:null,
                    houseId:null,
                    remarks:null
                },
                houselist:null
            },
            methods: {
                doSave(){
                    var uid = localStorage.getItem("uid");
                    if(this.userId != null){ //修改
                        var url='http://localhost:8080/owner/update?uid='+uid;
                        axios.post(url,this.owner).then(response =>{
                            alert(response.data.msg);
                            window.parent.main_right.location.href="owner_list.html";
                        });
                    } else { //保存
                        var url='http://localhost:8080/owner/insert?uid='+uid;
                        axios.post(url,this.owner).then(response =>{
                            alert(response.data.msg);
                            window.parent.main_right.location.href="owner_list.html";
                        });
                    }


                },
                doCancel(){
                   history.go(-1);
                }
            },
            created: function () {
                var uid = localStorage.getItem("uid");
                var url = window.location.href;
                if(url.indexOf('id') != -1){
                    this.userId = url.substring(url.indexOf('=')+1);
                }
                console.log("接收传参-用户ID："+this.userId);
                if(this.userId != null){
                    this.title = "修改业主信息";
                    var url='http://localhost:8080/house/all?uid='+uid;
                    axios.get(url).then(response =>{
                       if(response.data.code==200){
                           this.houselist=response.data.data;
                       }else {
                           alert(response.data.msg);
                       }
                    });
                    var url='http://localhost:8080/owner/selectOne?id='+this.userId+'&uid='+uid;
                    axios.get(url).then(response =>{
                        console.log(response.data.data);
                        this.owner=response.data.data;
                    });
                } else {
                    this.title = "添加业主";
                    var url='http://localhost:8080/house/all?uid='+uid;
                    axios.get(url).then(response =>{
                        if(response.data.code==200){
                            this.houselist=response.data.data;
                        }else {
                            alert(response.data.msg);
                        }
                    });
                }
            }
        });
    </script>
</body>
</html>